﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using Microsoft.Phone.Shell;
using Utilities.Menu;
using System.Collections.ObjectModel;
using EnhancedTimer.Resources;
using Services.Menu;
using Utilities;

namespace EnhancedTimer
{
    public class StateManager : AStateManager
    {
        Game game;
        public bool isTrial;
        public GraphicsDeviceManager graphics;
        public OptionsState OptionsState;
        public ArrayList<SoundEffectInstance> Sounds;
        public StateManager(Game game, GraphicsDeviceManager graphics)
            : base(game, graphics)
        {
            this.game = game;
            this.graphics = graphics;
            
            OptionsState = Storage.GetOptionsState();
            //if (OptionsState.allowRunScreenOff)
                PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
            //else
            //    PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled;
            
            isTrial = Guide.IsTrialMode;
        }
        public void LoadAlertSounds()
        {
            Sounds = new ArrayList<SoundEffectInstance>();
            for (int i = 1; i <= 10; i++)
            {
                Sounds.Add(game.Content.Load<SoundEffect>("Sounds/Alert " + i).CreateInstance());
            }
        }
        TimerMenu timerMenu;
        public override void SelectMainMenu()
        {
            game.Components.Clear();
            timerMenu = new TimerMenu(game, graphics);
            game.Components.Add(timerMenu);
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        public void SelectOptionsMenu()
        {
            game.Components.Clear();
            game.Components.Add(new OptionsMenu(game));

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public void Resume(bool newTimer)
        {
            game.Components.Clear();
            game.Components.Add(timerMenu);
            timerMenu.Resume(newTimer);
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public void SelectCreditsMenu()
        {
            game.Components.Clear();

            game.Components.Add(new CreditsMenu(game));
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }


        public void SelectSetTimerMenu()
        {
            game.Components.Clear();

            game.Components.Add(new NewTimerMenu(game));
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        public void SetNewTimer(TimeSpan time, TimerStatus status)
        {
            Resume(true);
            timerMenu.SetNewTimer(time, status);
            
        }
        public void OpenHourSelector(Action<DateTime> closeSelector, Action abortSelector, TimeSpan defaultTimeSpan, String title)
        {
            game.Components.Add(new TimeSelectorScreen(game, closeSelector, abortSelector, defaultTimeSpan, HourSelectorType.HoursMinutes, title));
            /*
            var months_names = new ArrayList<string>();
            var days_names = new ArrayList<string>();
            
            days_names.Add(Selector.Day_0_Monday);
            days_names.Add(Selector.Day_1_Tuesday);
            days_names.Add(Selector.Day_2_Wednesday);
            days_names.Add(Selector.Day_3_Thursday);
            days_names.Add(Selector.Day_4_Friday);
            days_names.Add(Selector.Day_5_Saturday);
            days_names.Add(Selector.Day_6_Sunday);

            months_names.Add(Selector.Month_00_January);
            months_names.Add(Selector.Month_01_February);
            months_names.Add(Selector.Month_02_March);
            months_names.Add(Selector.Month_03_April);
            months_names.Add(Selector.Month_04_May);
            months_names.Add(Selector.Month_05_June);
            months_names.Add(Selector.Month_06_July);
            months_names.Add(Selector.Month_07_August);
            months_names.Add(Selector.Month_08_September);
            months_names.Add(Selector.Month_09_October);
            months_names.Add(Selector.Month_10_November);
            months_names.Add(Selector.Month_11_December);

            game.Components.Add(new DateSelectorScreen(game, closeSelector, abortSelector, DateTime.Now , DateSelectorType.YearsMouthsDays, title, months_names, days_names));
            //game.Components.Add(new LoadingComponent(game, Color.Red, Color.Black * 0.8f));
             */
        }
    }
    
}
